package com.qfedu.Action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.biz.GoodsBiz;
import com.qfedu.entity.GoodsEntity;
import com.qfedu.entity.GoodsTypeEntity;
import com.qfedu.entity.Page;
import com.qfedu.enumeration.Enumtion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsAction {
@Autowired
    private GoodsBiz goodsBiz;

@RequestMapping("/index")
    public String doQueryAllGoods(GoodsEntity goodsEntity, HttpServletRequest request){
    //查询商品总数，默认查八个
    PageHelper.startPage(Enumtion.ENUM_ONE,Enumtion.ENUM_eight);
    List<GoodsEntity> goodsEntities = goodsBiz.queryAllGoods(goodsEntity);
   request.setAttribute("goodlist",goodsEntities);
   //查找商品类型默认查3个
    PageHelper.startPage(Enumtion.ENUM_ONE,Enumtion.ENUM_three);
    List<GoodsTypeEntity> goodsTypeEntities = goodsBiz.queryAllGoodsType(null);
    request.setAttribute("typelist",goodsTypeEntities);
    return "index";
}
//按条件查询
//@RequestMapping("/querygoods.do")
//    public String doQueryGoods(GoodsEntity goodsEntity,HttpServletRequest request){
//        request.setAttribute("term",goodsEntity);
//        PageHelper.startPage(goodsEntity.getPageNum(),goodsEntity.getPageSize());
//        List<GoodsEntity> goodsEntities = goodsBiz.queryAllGoods(goodsEntity);
//        PageInfo<GoodsEntity> pageinfo =new PageInfo<GoodsEntity>(goodsEntities);
//        request.setAttribute("pageinfo",pageinfo);
//        return "prolist";
//    }


    //自定义分页查询
    @RequestMapping("/querygoods.do")
    public String doQueryGoods(GoodsEntity goodsEntity,HttpServletRequest request){
        request.setAttribute("term",goodsEntity);
       int startindex= (goodsEntity.getPageNum()-1)*goodsEntity.getPageSize();
        goodsEntity.setStartindex(startindex);
        Page pageinfo = goodsBiz.queryByPage(goodsEntity);
        request.setAttribute("pageinfo",pageinfo);
        return "prolist";
    }








@RequestMapping("/queryDetailById.do")
    public String doQueryById(GoodsEntity goodsEntity,HttpServletRequest request){
    List<GoodsEntity> goodsEntities = goodsBiz.queryAllGoods(goodsEntity);
    GoodsEntity goodsEntity1 = goodsEntities.get(0);
    request.setAttribute("thegood",goodsEntity1);
    return "prodetail";

}

@RequestMapping("shopCarDetail.to")
    public String doShopCarDetail(HttpServletRequest request){
    return "shopcar";
}
}
